from fastapi import APIRouter, HTTPException, Depends
from typing import List
from sqlalchemy.orm import Session
from schema import System_Log_schema
from controller import System_Log_controller
from database.db_init import get_db


router = APIRouter()


@router.post("/System_Log", response_model=System_Log_schema.System_log, tags=["System_Log"])
async def create_System_Log(System_Log: System_Log_schema.System_log, db: Session = Depends(get_db)):
    return System_Log_controller.create_System_Log(db, System_Log)


@router.get("/System_Log/{ID}", response_model=System_Log_schema.System_log, tags=["System_Log"])
async def get_System_Log(ID: int, db: Session = Depends(get_db)):
    db_System_Log = System_Log_controller.get_System_Log(db, ID)
    if db_System_Log is None:
        raise HTTPException(status_code=404, detail="System_Log not found")
    return db_System_Log


@router.get("/System_Log", response_model=List[System_Log_schema.System_log], tags=["System_Log"])
async def get_all_System_Log(db: Session = Depends(get_db)):
    all_db_System_Log = System_Log_controller.get_all_System_Log(db)
    if all_db_System_Log is None:
        raise HTTPException(status_code=404, detail="0 System_Log found, empty table")
    return all_db_System_Log


@router.put("/System_Log/{ID}", tags=["System_Log"])
async def update_System_Log(ID: int, field_name: str, field_value: str, db: Session = Depends(get_db)):
    try:
        db_System_Log = System_Log_controller.update_System_Log(db, ID, field_name, field_value)
        if db_System_Log is None:
            raise HTTPException(status_code=404, detail="System_Log not found")
        return db_System_Log
    except ValueError as error:
        raise HTTPException(status_code=400, detail=str(error))


@router.delete("/System_Log/{ID}", tags=["System_Log"])
async def delete_System_Log(ID: int, db: Session = Depends(get_db)):
    is_deleted = System_Log_controller.delete_System_Log(db, ID)
    if is_deleted is None:
        raise HTTPException(status_code=404, detail=f'Sample {ID} does not exist in database')
    else:
        return {'message': f'Successfully delete sample {ID}'}
